<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222"><meta name="generator" content="Hexo 5.4.2">

  <link rel="apple-touch-icon" sizes="180x180" href="/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/images/logo.svg" color="#222">

<link rel="stylesheet" href="/css/main.css">



<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" integrity="sha256-HtsXJanqjKTc8vVQjO4YMhiqFoXkfBsjBWcX91T1jr8=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/animate.css/3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"xiaoqingming18.github.io","root":"/","images":"/images","scheme":"Muse","darkmode":false,"version":"8.15.1","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":{"enable":false,"style":null},"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"menu_item":"fadeInDown","post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"}}</script><script src="/js/config.js"></script>

    <meta name="description" content="JavaScript函数定义JavaScript的函数实际上是对象。每个函数都是Function类型的实例，有自己的属性和方法，函数名就是指向函数对象的指针。 JavaScript的函数有几种定义方式，最常用的便是函数声明： 123function sum(num1,num2) {    return num1 + num2;} 另一种定义函数的方式是函数表达式： 123let sum &#x3D; fun">
<meta property="og:type" content="article">
<meta property="og:title" content="JavaScript：函数">
<meta property="og:url" content="http://xiaoqingming18.github.io/2023/07/14/JavaScript%EF%BC%9A%E5%87%BD%E6%95%B0/index.html">
<meta property="og:site_name" content="鸣蜩十七">
<meta property="og:description" content="JavaScript函数定义JavaScript的函数实际上是对象。每个函数都是Function类型的实例，有自己的属性和方法，函数名就是指向函数对象的指针。 JavaScript的函数有几种定义方式，最常用的便是函数声明： 123function sum(num1,num2) {    return num1 + num2;} 另一种定义函数的方式是函数表达式： 123let sum &#x3D; fun">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2023-07-14T05:44:34.000Z">
<meta property="article:modified_time" content="2023-07-14T06:52:22.692Z">
<meta property="article:author" content="鸣蜩十七">
<meta property="article:tag" content="-JavaScript基础 -JavaScript函数">
<meta name="twitter:card" content="summary">


<link rel="canonical" href="http://xiaoqingming18.github.io/2023/07/14/JavaScript%EF%BC%9A%E5%87%BD%E6%95%B0/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"http://xiaoqingming18.github.io/2023/07/14/JavaScript%EF%BC%9A%E5%87%BD%E6%95%B0/","path":"2023/07/14/JavaScript：函数/","title":"JavaScript：函数"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>JavaScript：函数 | 鸣蜩十七</title>
  








  <noscript>
    <link rel="stylesheet" href="/css/noscript.css">
  </noscript>

<style>.github-emoji { position: relative; display: inline-block; width: 1.2em; min-height: 1.2em; overflow: hidden; vertical-align: top; color: transparent; }  .github-emoji > span { position: relative; z-index: 10; }  .github-emoji img, .github-emoji .fancybox { margin: 0 !important; padding: 0 !important; border: none !important; outline: none !important; text-decoration: none !important; user-select: none !important; cursor: auto !important; }  .github-emoji img { height: 1.2em !important; width: 1.2em !important; position: absolute !important; left: 50% !important; top: 50% !important; transform: translate(-50%, -50%) !important; user-select: none !important; cursor: auto !important; } .github-emoji-fallback { color: inherit; } .github-emoji-fallback img { opacity: 0 !important; }</style>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <div class="column">
      <header class="header" itemscope itemtype="http://schema.org/WPHeader"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/" class="brand" rel="start">
      <i class="logo-line"></i>
      <p class="site-title">鸣蜩十七</p>
      <i class="logo-line"></i>
    </a>
      <p class="site-subtitle" itemprop="description">鸣蜩十七是和女朋友谈恋爱的日子</p>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger" aria-label="搜索" role="button">
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu"><li class="menu-item menu-item-home"><a href="/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li><li class="menu-item menu-item-about"><a href="/about/" rel="section"><i class="fa fa-user fa-fw"></i>关于</a></li><li class="menu-item menu-item-tags"><a href="/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li><li class="menu-item menu-item-categories"><a href="/categories/" rel="section"><i class="fa fa-th fa-fw"></i>分类</a></li><li class="menu-item menu-item-archives"><a href="/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li><li class="menu-item menu-item-schedule"><a href="/schedule/" rel="section"><i class="fa fa-calendar fa-fw"></i>日程表</a></li><li class="menu-item menu-item-sitemap"><a href="/sitemap.xml" rel="section"><i class="fa fa-sitemap fa-fw"></i>站点地图</a></li><li class="menu-item menu-item-commonweal"><a href="/404/" rel="section"><i class="fa fa-heartbeat fa-fw"></i>公益 404</a></li>
  </ul>
</nav>




</header>
        
  
  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#JavaScript%E5%87%BD%E6%95%B0%E5%AE%9A%E4%B9%89"><span class="nav-text">JavaScript函数定义</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%AE%AD%E5%A4%B4%E5%87%BD%E6%95%B0"><span class="nav-text">箭头函数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%90%8D%E4%B8%8Ename%E5%B1%9E%E6%80%A7"><span class="nav-text">函数名与name属性</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E5%87%BD%E6%95%B0%E5%90%8D"><span class="nav-text">函数名</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#name%E5%B1%9E%E6%80%A7"><span class="nav-text">name属性</span></a></li></ol></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E7%90%86%E8%A7%A3JavaScript%E5%87%BD%E6%95%B0%E7%9A%84%E5%8F%82%E6%95%B0"><span class="nav-text">理解JavaScript函数的参数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B2%A1%E6%9C%89%E9%87%8D%E8%BD%BD"><span class="nav-text">没有重载</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0%E5%80%BC"><span class="nav-text">默认参数值</span></a><ol class="nav-child"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E4%B8%BA%E5%8F%82%E6%95%B0%E8%AE%BE%E7%BD%AE%E9%BB%98%E8%AE%A4%E5%80%BC"><span class="nav-text">为参数设置默认值</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#%E9%BB%98%E8%AE%A4%E5%8F%82%E6%95%B0%E4%BD%9C%E7%94%A8%E5%9F%9F%E4%B8%8E%E6%9A%82%E6%97%B6%E6%80%A7%E6%AD%BB%E5%8C%BA"><span class="nav-text">默认参数作用域与暂时性死区</span></a></li></ol></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
  <p class="site-author-name" itemprop="name">鸣蜩十七</p>
  <div class="site-description" itemprop="description">记录我的学习、生活和恋爱日常</div>
</div>
<div class="site-state-wrap animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/archives/">
          <span class="site-state-item-count">43</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-categories">
          <a href="/categories/">
        <span class="site-state-item-count">8</span>
        <span class="site-state-item-name">分类</span></a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/tags/">
        <span class="site-state-item-count">37</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>

        </div>
      </div>
    </div>

    
  </aside>


    </div>

    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="http://xiaoqingming18.github.io/2023/07/14/JavaScript%EF%BC%9A%E5%87%BD%E6%95%B0/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="/images/avatar.gif">
      <meta itemprop="name" content="鸣蜩十七">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="鸣蜩十七">
      <meta itemprop="description" content="记录我的学习、生活和恋爱日常">
    </span>

    <span hidden itemprop="post" itemscope itemtype="http://schema.org/CreativeWork">
      <meta itemprop="name" content="JavaScript：函数 | 鸣蜩十七">
      <meta itemprop="description" content="">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          JavaScript：函数
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>
      

      <time title="创建时间：2023-07-14 13:44:34 / 修改时间：14:52:22" itemprop="dateCreated datePublished" datetime="2023-07-14T13:44:34+08:00">2023-07-14</time>
    </span>
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-folder"></i>
      </span>
      <span class="post-meta-item-text">分类于</span>
        <span itemprop="about" itemscope itemtype="http://schema.org/Thing">
          <a href="/categories/%E5%89%8D%E7%AB%AF/" itemprop="url" rel="index"><span itemprop="name">-前端</span></a>
        </span>
    </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <h1 id="JavaScript函数定义"><a href="#JavaScript函数定义" class="headerlink" title="JavaScript函数定义"></a>JavaScript函数定义</h1><p>JavaScript的函数实际上是对象。每个函数都是<code>Function</code>类型的实例，有自己的属性和方法，函数名就是指向函数对象的指针。</p>
<p>JavaScript的函数有几种定义方式，最常用的便是函数声明：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">num1,num2</span>) {</span><br><span class="line">    <span class="keyword">return</span> num1 + num2;</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>另一种定义函数的方式是函数表达式：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> sum = <span class="keyword">function</span>(<span class="params">num1,num2</span>) {</span><br><span class="line">    <span class="keyword">return</span> num1 + num2;</span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>用函数表达式定义函数相当于将用<code>function</code>定义的函数实例的引用赋给<code>sum</code>对象变量。</p>
<h1 id="箭头函数"><a href="#箭头函数" class="headerlink" title="箭头函数"></a>箭头函数</h1><p>任何使用函数的地方也可以用箭头函数代替，箭头函数的形式如下：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title function_">sum</span> = (<span class="params">a,b</span>) =&gt; {</span><br><span class="line">    <span class="keyword">return</span> a + b;</span><br><span class="line">};</span><br></pre></td></tr></tbody></table></figure>
<p>如果只有一个参数，可以省略括号：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title function_">double</span> = x =&gt; {<span class="keyword">return</span> x ** <span class="number">2</span>;};</span><br></pre></td></tr></tbody></table></figure>
<p>没有参数时不可以省略括号：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">let</span> <span class="title function_">getRandom</span> = (<span class="params"></span>) =&gt; {<span class="keyword">return</span> <span class="title class_">Math</span>.<span class="title function_">random</span>();};</span><br></pre></td></tr></tbody></table></figure>
<p>箭头函数虽然语法简洁，但也有很多场合不适用。箭头函数不能使用<code>arguments</code>、<code>super</code>和<code>new.target</code>，也不能用作构造函数。此外，箭头函数也没有<code>prototype</code>属性。</p>
<h1 id="函数名与name属性"><a href="#函数名与name属性" class="headerlink" title="函数名与name属性"></a>函数名与<code>name</code>属性</h1><h2 id="函数名"><a href="#函数名" class="headerlink" title="函数名"></a>函数名</h2><p>函数名就是指向函数的指针，所以一个函数可以有多个函数名，就像一个对象可以被多个对象变量引用一样：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">sum</span>(<span class="params">num1,num2</span>) {</span><br><span class="line">    <span class="keyword">return</span> num1 + num2;</span><br><span class="line">}</span><br><span class="line"></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">sum</span>(<span class="number">10</span>,<span class="number">10</span>)); <span class="comment">// 20</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">let</span> anotherSum = sum;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">anotherSum</span>(<span class="number">10</span>,<span class="number">10</span>)); <span class="comment">// 20</span></span><br><span class="line"></span><br><span class="line">sum = <span class="literal">null</span>;</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">anotherSum</span>(<span class="number">10</span>,<span class="number">10</span>)); <span class="comment">// 20</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="name属性"><a href="#name属性" class="headerlink" title="name属性"></a><code>name</code>属性</h2><p>所有的函数都有一个<code>name</code>属性，该属性保存的是该函数名称的字符串。</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) {}</span><br><span class="line">foo.<span class="property">name</span> <span class="comment">// "foo"</span></span><br></pre></td></tr></tbody></table></figure>
<p>这个属性早就被浏览器广泛支持，但是直到 ES6，才将其写入了标准。但是ES6对这个属性的一些行为做出了一些修改。分以下四种情况：</p>
<p><strong>函数被声明为一个命名函数</strong>：这种情况ES5和ES6语法一致，<code>name</code>属性返回该函数的函数名：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) {}</span><br><span class="line"><span class="keyword">typeof</span> foo <span class="comment">// “function”</span></span><br><span class="line">foo.<span class="property">name</span> <span class="comment">// “foo”</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>创建一个匿名函数，并赋值给一个变量</strong>：在ES5中，该函数的<code>name</code>属性为“”，而 ES6 的<code>name</code>属性会返回实际的函数名：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> f = <span class="keyword">function</span> (<span class="params"></span>) {};</span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line">f.<span class="property">name</span> <span class="comment">// “”</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line">f.<span class="property">name</span> <span class="comment">// “f”</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>创建一个匿名函数，将其赋值给一个<code>window</code>的属性</strong>：在ES5中，该函数的<code>name</code>属性返回函数名（window下的赋值属性名），而 ES6 的<code>name</code>属性会返回“”：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="variable language_">window</span>.<span class="property">f</span> = <span class="keyword">function</span> (<span class="params"></span>) { };</span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line">f.<span class="property">name</span> <span class="comment">// “f”</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line">f.<span class="property">name</span> <span class="comment">// “”</span></span><br></pre></td></tr></tbody></table></figure>
<p><strong>创建一个命名函数，将其赋值给一个变量或<code>window</code>下的属性</strong>：这种情况ES5和ES6语法一致，<code>name</code>均返回函数的字面量名称，与将函数进行赋值的变量或属性名称无关：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> f = <span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) {};</span><br><span class="line"><span class="variable language_">window</span>.<span class="property">f</span> = <span class="keyword">function</span> <span class="title function_">foo</span>(<span class="params"></span>) { };</span><br><span class="line"><span class="comment">// ES5</span></span><br><span class="line">f.<span class="property">name</span> <span class="comment">// “foo”</span></span><br><span class="line"><span class="comment">// ES6</span></span><br><span class="line">f.<span class="property">name</span> <span class="comment">// “foo”</span></span><br></pre></td></tr></tbody></table></figure>
<h1 id="理解JavaScript函数的参数"><a href="#理解JavaScript函数的参数" class="headerlink" title="理解JavaScript函数的参数"></a>理解JavaScript函数的参数</h1><p>与其他语言不同，JavaScript即不关心参数的个数也不关心参数的数据类型。因为JavaScript函数的参数在内部表现为一个数组。在使用<code>function</code>关键字定义函数（非箭头函数）时，可以在函数内部访问<code>arguments</code>对象，从中取得传进来的每个参数值。</p>
<p>看下面的例子：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">fucntion <span class="title function_">sayHi</span>(<span class="params">name,message</span>) {</span><br><span class="line">    <span class="variable language_">console</span>,<span class="title function_">log</span>(<span class="string">"Hello "</span> + name + <span class="string">","</span> + message);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>上面的函数可以写成下面这样：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">sayHi</span>(<span class="params"></span>) {</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="string">"Hello "</span> + name + <span class="string">","</span> + <span class="variable language_">arguments</span>[<span class="number">1</span>]);</span><br><span class="line">}</span><br></pre></td></tr></tbody></table></figure>
<p>上面的例子可以看出，在JavaScript中，形参不是必须的，传给函数的参数值都可以通过<code>arguments</code>对象获取。</p>
<p>还有很重要的一点，<strong><code>arguments</code>对象的值始终与命名参数（形参）保持同步</strong>。<code>arguments</code>对象的值会自动同步到对应的命名参数，例如修改<code>arguments[1]</code>的值就会同时修改第二个命名参数的值。但是<code>arguments</code>和命名参数在内存中还是分开的，只不过会保持同步而已。</p>
<p>另外还要注意一点：如果只给函数传入了一个参数，那么修改<code>arguments[1]</code>的值并不会同步到第二个命名参数，因为<code>arguments</code>对象的长度是根据传入的参数（实参）决定的，而不是由命名参数（形参）决定的。</p>
<p>最后一点，箭头函数没有<code>arguments</code>对象。</p>
<h1 id="没有重载"><a href="#没有重载" class="headerlink" title="没有重载"></a>没有重载</h1><p>JavaScript中没有函数的重载，后定义的函数会覆盖先定义的函数。但是可以通过检查参数的类型和数量，然后在同一个函数内部执行不同的逻辑。</p>
<h1 id="默认参数值"><a href="#默认参数值" class="headerlink" title="默认参数值"></a>默认参数值</h1><h2 id="为参数设置默认值"><a href="#为参数设置默认值" class="headerlink" title="为参数设置默认值"></a>为参数设置默认值</h2><p>在函数定义的命名参数后面用<code>=</code>显式地赋值，可以为命名参数指定一个默认值：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">makeKing</span>(<span class="params">name = <span class="string">'Henry'</span></span>) {</span><br><span class="line">    <span class="keyword">return</span> <span class="string">`King <span class="subst">${name}</span> VIII`</span>;</span><br><span class="line">}</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">makeKing</span>(<span class="string">'Louis'</span>)); <span class="comment">// 'King Louis VIII'</span></span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(<span class="title function_">makeKing</span>()); <span class="comment">// 'King Henry VIII'</span></span><br></pre></td></tr></tbody></table></figure>
<h2 id="默认参数作用域与暂时性死区"><a href="#默认参数作用域与暂时性死区" class="headerlink" title="默认参数作用域与暂时性死区"></a>默认参数作用域与暂时性死区</h2><p>参数是按顺序初始化的，所以后定义默认值的参数可以引用先定义的参数，就像下面这样：</p>
<figure class="highlight javascript"><table><tbody><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">function</span> <span class="title function_">makeKing</span>(<span class="params">name = <span class="string">'Henry'</span>,numerals = name</span>) {}</span><br></pre></td></tr></tbody></table></figure>
<p>参数初始化顺序遵循<strong>暂时性死区</strong>规则，即前面定义的参数不能引用后面定义的，否则就会抛出错误。</p>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/tags/JavaScript%E5%9F%BA%E7%A1%80-JavaScript%E5%87%BD%E6%95%B0/" rel="tag"># -JavaScript基础 -JavaScript函数</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/2023/07/12/%E8%AE%A1%E7%AE%97%E6%9C%BA%E7%BD%91%E7%BB%9C%EF%BC%9A%E7%BD%91%E7%BB%9C%E6%98%AF%E6%80%8E%E6%A0%B7%E8%BF%9E%E6%8E%A5%E7%9A%84/" rel="prev" title="计算机网络：网络是怎样连接的">
                  <i class="fa fa-chevron-left"></i> 计算机网络：网络是怎样连接的
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/2023/07/16/JavaScript%EF%BC%9A%E6%9E%84%E9%80%A0%E5%87%BD%E6%95%B0%E4%B8%8E%E5%8E%9F%E5%9E%8B%E3%80%81%E5%8E%9F%E5%9E%8B%E9%93%BE/" rel="next" title="JavaScript：构造函数与原型、原型链">
                  JavaScript：构造函数与原型、原型链 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">鸣蜩十七</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/muse/" rel="noopener" target="_blank">NexT.Muse</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>
  <div class="sidebar-dimmer"></div>
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up fa-lg"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


  
  <script src="https://cdnjs.cloudflare.com/ajax/libs/animejs/3.2.1/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/js/comments.js"></script><script src="/js/utils.js"></script><script src="/js/motion.js"></script><script src="/js/schemes/muse.js"></script><script src="/js/next-boot.js"></script>

  




  




  

  <script class="next-config" data-name="enableMath" type="application/json">true</script><script class="next-config" data-name="mathjax" type="application/json">{"enable":true,"tags":"none","js":{"url":"https://cdnjs.cloudflare.com/ajax/libs/mathjax/3.2.2/es5/tex-mml-chtml.js","integrity":"sha256-MASABpB4tYktI2Oitl4t+78w/lyA+D7b/s9GEP0JOGI="}}</script>
<script src="/js/third-party/math/mathjax.js"></script>



</body>
</html>
